﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;
using PMSGSC.DAL;
using PMSGSC.BLL;
using PMSGSC.Common;

namespace PMSGSC.App
{
    public partial class frmFilterIOList : DevExpress.XtraEditors.XtraForm
    {
        public frmFilterIOList(frmMain objMain)
        {
            InitializeComponent();
            deTo.DateTime = DateTime.Now.Date;
            deFrom.DateTime = deTo.DateTime.Date.AddMonths(-1);
            main = objMain;
        }
        frmShowIOList ioList;
        List<Invoice> invoiceList = new List<Invoice>();
        frmProcessBar pb = new frmProcessBar();
        private bool isStop = true;
        frmMain main;
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (isStop)
            {
                this.Close();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            if (isStop)
            {

                Thread thread = new Thread(new ThreadStart(() =>
                {
                    LoadSkinToForm();
                    pb.ShowDialog();
                }
                ));
                thread.Start();

                if (ucLoadEquipment1.lbid.Text == "")
                    invoiceList = Invoice_BLL.GetIOList(deFrom.DateTime, deTo.DateTime, 0, comboBoxEdit1.SelectedIndex, frmMain.ship.Ship_ID);
                else
                    invoiceList = Invoice_BLL.GetIOList(deFrom.DateTime, deTo.DateTime,
                        int.Parse(ucLoadEquipment1.lbid.Text), comboBoxEdit1.SelectedIndex, frmMain.ship.Ship_ID);


                if (invoiceList.Count > 0)
                {
                    isStop = false;
                    ioList = new frmShowIOList(invoiceList, main);
                    ioList.MdiParent = main;
                    ioList.Show();

                    if (pb.InvokeRequired)
                    {
                        pb.BeginInvoke(new Action(() => pb.Close()));
                        isStop = true;
                    }
                    this.Close();
                }
                else
                {
                    if (pb.InvokeRequired)
                    {
                        pb.BeginInvoke(new Action(() => pb.Close()));
                        isStop = true;
                    }
                    XtraMessageBox.Show("No result in display", "Infomation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                XtraMessageBox.Show("Please enter infomation search component into Textbox", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }


        }
        private static void LoadSkinToForm()
        {
            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.LookAndFeel.DefaultLookAndFeel obj = new DevExpress.LookAndFeel.DefaultLookAndFeel();
            try
            {
                LookAndFeelSettings.Load("LookAndFeelSettings.dat");
            }
            catch
            {
                obj.LookAndFeel.SetSkinStyle("Blue");
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            deTo.DateTime = DateTime.Now.Date;
            deFrom.DateTime = deTo.DateTime.Date.AddMonths(-1);
            comboBoxEdit1.SelectedIndex = 0;
        }
    }
}